<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Course;
use App\Models\CourseRecord;
use App\Services\CourseService;
use App\Services\OrderService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CourseController extends Controller
{

    protected $CourseService;
    public function __construct(CourseService $CourseService)
    {
        $this->CourseService = $CourseService;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $title = $request->title;
        $type_id = $request->type_id;
        $tag = $request->tag;
        $pageSize = $request->pageSize ?: 10;
        $page = $request->page ?: 1;
        $skip = $page * $pageSize - $pageSize;

        $sql = $this->CourseService->get($title,$type_id);
        $sql->select('id','title','cover','type_id','price','original_price','sort','clicks','status','shop_type','tag');
        $sql->when($tag, function ($query, $tag) {
            return $$query->where('tag',$tag);
        });
        $total = $sql->count();
        $data = $sql->skip($skip)->take($pageSize)->orderBy('created_at','desc')->get();
        return response()->json(['code' => 200, 'message' => '操作成功','page'=>$page,'pageSize'=>$pageSize,'total'=>$total,'data'=>$data]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function pay(Request $request)
    {
        $order = new OrderService();
        $user = Auth::user();
        $data = $order->payCourse($user->id,'course');
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function record(Request $request)
    {
        $user = Auth::user();
        $data = $this->CourseService->record($user->id);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request)
    {
        $data = Course::find($request->id);
        $videos = $data->videos;
        //判断是否已经购买
        $data->isPay = false;
        if (Auth::guard('api')->check()) {
            $order = new OrderService();
            $user = Auth::guard('api')->user();
            $isPay = $order->isPay($user->id,$request->id,'course');
            $data->isPay = $isPay;
        }

        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data,'videos'=>$videos]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function study(Request $request)
    {
        $user = Auth::user();
        $data =  CourseRecord::where('user_id',$user->id)->first();
        if(!$data){
            $data = new CourseRecord();
            $data->user_id = $user->id;
        }
        $data->course_id = $request->course_id;
        $data->video_id = $request->video_id;
        $data->progress = $request->progress ?? 0;
        $data->last_position = $request->last_position ?? 0;
        $data->save();
        return response()->json(['code' => 200, 'message' => '操作成功']);
    }
}
